11
Importance of Data Structure Encapsulation
Version A:
z.Re = x.Re + y.Re
z.Im = x.Im + y.Im
Version B:
z[0] = x[0] + y[0]
z[1] = x[1] + y[1]
For example the addition of two complex numbers requires you to perform an addition for each part. Consequently, you must access the value of each part which is different for each version. By providing an operation “add'' you can encapsulate these details from its actual use. In an application context you simply “add two complex numbers'' regardless of how this functionality is actually achieved.
Once you have created an ADT for complex numbers, say Complex, you can use it in the same way like well-known data types such as integers.
Let's summarize this: The separation of data structures and operations and the constraint to only access the data structure via a well-defined interface allows you to choose data structures appropriate for the application environment.